package assistant.dao.daoImpl;

import achievement.domain.SzlBook;
import assistant.dao.EvaluateDao;
import assistant.domain.Evaluate;
import assistant.domain.Wish;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2022/12/17 17:53
 * @Author lihu
 */
public class EvaluateDaoImpl implements EvaluateDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;

    @Override
    public void insert(Evaluate evaluate) {
        String sql = "insert into lh_evaluate\n" +
                "        (teacher_id,student_id, course_id, `result`, finalselect_time, self_evaluate, teacher_evaluate, evaluate_time) \n" +
                "values(?,?,?,?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, evaluate.getTeacherId());
            psmt.setLong(2, evaluate.getStudentId());
            psmt.setLong(3, evaluate.getCourseId());
            psmt.setString(4, evaluate.getResult());
            psmt.setObject(5, evaluate.getFinalselectTime());
            psmt.setString(6, evaluate.getSelfEvaluate());
            psmt.setString(7, evaluate.getTeacherEvaluate());
            psmt.setObject(8, evaluate.getEvaluateTime());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void updateById(Evaluate evaluate) {
        String sql = "update lh_evaluate set";
        List<Object> param = new ArrayList<>();
        if (evaluate == null) {
            return;
        } else {
            if (evaluate.getTeacherId() != null) {
                sql += " teacher_id = ?,";
                param.add(evaluate.getTeacherId());
            }
            if (evaluate.getStudentId() != null) {
                sql += " student_id= ?,";
                param.add(evaluate.getStudentId());
            }
            if (evaluate.getCourseId() != null) {
                sql += " course_id = ?,";
                param.add(evaluate.getCourseId());
            }
            if (evaluate.getResult() != null) {
                sql += " `result` = ?,";
                param.add(evaluate.getResult());
            }
            if (evaluate.getFinalselectTime() != null) {
                sql += " finalselect_time = ?,";
                param.add(evaluate.getFinalselectTime());
            }
            if (evaluate.getSelfEvaluate() != null) {
                sql += " self_evaluate = ?,";
                param.add(evaluate.getSelfEvaluate());
            }
            if (evaluate.getTeacherEvaluate() != null) {
                sql += "  teacher_evaluate = ?,";
                param.add(evaluate.getTeacherEvaluate());
            }
            if (evaluate.getEvaluateTime() != null) {
                sql += "  evaluate_time = ?,";
                param.add(evaluate.getEvaluateTime());
            }
        }
        sql = StrUtil.strip(sql, null, ",");
        sql += " where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            psmt.setLong(param.size() + 1, evaluate.getId());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from lh_evaluate where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public Evaluate selectById(Long id) {
/*        String sql = "insert into lh_evaluate\n" +
                "        (teracher_id, ,student_id, course_id, `result`, finalselect_time, self_evaluate, teacher_evaluate, evaluate_time) \n" +
                "values(?,?,?,?,?,?,?,?)";
                */
        String sql = "select * from lh_evaluate where id = ?";
        Evaluate evaluate = new Evaluate();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            rs = psmt.executeQuery();
            while (rs.next()) {
                evaluate.setId(rs.getLong(1));
                evaluate.setTeacherId(rs.getLong(2));
                evaluate.setStudentId(rs.getLong(3));
                evaluate.setCourseId(rs.getLong(4));
                evaluate.setResult(rs.getString(5));
                evaluate.setFinalselectTime(rs.getTime(6));
                evaluate.setSelfEvaluate(rs.getString(7));
                evaluate.setTeacherEvaluate(rs.getString(8));
                evaluate.setEvaluateTime(rs.getTime(9));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return evaluate;
    }

    @Override
    public List<Evaluate> selectList(Evaluate evaluate) {
        String sql = "select * from lh_evaluate where 1=1";
        List<Evaluate> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (evaluate == null) {
            sql = "select * from lh_evaluate";
        } else {
            if (evaluate.getTeacherId() != null) {
                sql += " and teacher_id = ?";
                param.add(evaluate.getTeacherId());
            }
            if (evaluate.getStudentId() != null) {
                sql += " and student_id= ?";
                param.add(evaluate.getStudentId());
            }
            if (evaluate.getCourseId() != null) {
                sql += " and course_id = ?";
                param.add(evaluate.getCourseId());
            }
            if (evaluate.getResult() != null) {
                sql += " and `result` = ?";
                param.add(evaluate.getResult());
            }
            if (evaluate.getFinalselectTime() != null) {
                sql += " and finalselect_time = ?";
                param.add(evaluate.getFinalselectTime());
            }
            if (evaluate.getSelfEvaluate() != null) {
                sql += " and self_evaluate = ?";
                param.add(evaluate.getSelfEvaluate());
            }
            if (evaluate.getTeacherEvaluate() != null) {
                sql += " and  teacher_evaluate = ?";
                param.add(evaluate.getTeacherEvaluate());
            }
            if (evaluate.getEvaluateTime() != null) {
                sql += " and  evaluate_time = ?";
                param.add(evaluate.getEvaluateTime());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()) {
                Evaluate evaluate1 = new Evaluate();
                evaluate1.setId(rs.getLong(1));
                evaluate1.setTeacherId(rs.getLong(2));
                evaluate1.setStudentId(rs.getLong(3));
                evaluate1.setCourseId(rs.getLong(4));
                evaluate1.setResult(rs.getString(5));
                evaluate1.setFinalselectTime(rs.getTime(6));
                evaluate1.setSelfEvaluate(rs.getString(7));
                evaluate1.setTeacherEvaluate(rs.getString(8));
                evaluate1.setEvaluateTime(rs.getTime(9));
                list.add(evaluate1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return list;
    }
}
